<template>
  <commonDrawer
    :title="type==='edit'?'修改事件推送':'新增事件推送'"
    :drawer="drawer"
    @closeDraw="openOrCloseDrawer"
    @cancelFun="drawer=false"
    @save="save">
    <template slot="column">
      <el-form class="m7-form" :model="form" :rules="rules" ref="formRef" label-position="top" size="mini">
        <el-form-item label="事件类型" prop="type">
          <m7Select filterable  size="mini" v-model="form.eventType" placeholder="请选择" :options="typeArr"></m7Select>
        </el-form-item>
        <el-form-item label="推送地址" prop="pushUrl">
          <el-input size="mini" v-model.trim="form.pushUrl" placeholder="url必须要包含协议头http(s)://"></el-input>
        </el-form-item>
        <el-form-item label="说明" prop="description">
          <el-input size="mini" type="textarea" v-model.trim="form.description"  placeholder="请输入说明" autosize></el-input>
        </el-form-item>
      </el-form>
    </template>
  </commonDrawer>
</template>

<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator'
import { SystemConfigModule } from '@/store/modules/config/systemConfig'
import commonDrawer from '@/common/views/drawer/index.vue'
import { urlValidate } from '@/utils/validate'
import { deepClone } from '@/utils/common'

@Component({
  name: 'TimeDetial',
  components: { commonDrawer }
})
export default class TimeDetial extends Vue {
  [x: string]: any
  @Prop({ default: 'add' }) private type!: string
  private drawer: boolean = false // 抽屉的控制值
  private typeArr: object[] = [ // 事件类型下拉
    { label: '外呼振铃事件', value: 'dialoutRing' },
    { label: '外呼挂机事件', value: 'dialoutHangup' },
    { label: '来电振铃事件', value: 'incomingRing' },
    { label: '来电挂机事件', value: 'incomingHangup' },
    { label: '通话记录推送事件', value: 'cdr' },
    { label: '客户接通事件', value: 'link' },
    { label: '座席状态变化事件', value: 'callStatus' },
    { label: '媒体识别挂机原因事件', value: 'mediaFailreason' },
    { label: '电话条登录状态事件', value: 'loginStatus' },
    { label: '排队开始事件', value: 'QueueStart' },
    { label: '排队结束事件', value: 'QueueEnd' },
    { label: '备件变更', value: 'SALE_SPARE_CHANGE' },
    { label: '备件入库', value: 'SALE_SPARE_RK' },
    { label: '备件出库', value: 'SALE_SPARE_CK' },
    { label: '服务项目品类变更', value: 'SALE_SERVICE_PROJECT_ITEM_CHANGE' }
  ]

  private form: any = { // 初始表单
    _id: '',
    eventType: '1',
    pushUrl: '',
    description: ''
  }

  private validateHttp = (rule, value, callback) => { // 检测Url是否规范
    if (value !== '') {
      if (!urlValidate(value)) {
        callback(new Error('请输入正确的推送地址'))
      } else {
        callback()
      }
    } else {
      callback(new Error('请输入正确的推送地址'))
    }
  }

  private rules: object = { // 表单校验的规则
    pushUrl: [{ required: true, validator: this.validateHttp, trigger: 'blur' }]
  }

  public openOrCloseDrawer (data: any) { // 打开或者是关闭抽屉 || 父级调用
    this.drawer = !this.drawer
    this.$nextTick(() => {
      (this as any).$refs.formRef.clearValidate()
    })
    this.handleForm(data)
  }

  private handleForm (data: any) { // 打开抽屉时，整理form数据
    if (data) { // 编辑
      Object.keys(data).forEach(key => {
        if (this.form[key] !== undefined) {
          this.form[key] = data[key]
        }
      })
    } else { // 新增重置表单
      this.form = {
        _id: '',
        eventType: '',
        pushUrl: '',
        description: ''
      }
    }
  }

  private save () { // 保存
    (this as any).$refs.formRef.validate((valid: boolean) => {
      if (valid) {
        const actionFrom = deepClone(this.form)
        if (this.type === 'add') {
          delete actionFrom._id
        }
        const data = { url: '/base/action/general/docking/save', method: 'POST', data: actionFrom }
        SystemConfigModule.SystemCommonAction(data).then((res: any) => {
          if (res && res.success) {
            this.$emit('refresh', this.type === 'add' ? 'addSuccess' : null) // 外抛给父组件，用来刷新到第一页
            this.drawer = false
            this.$message({ message: '保存成功', type: 'success' })
          }
        })
      } else {
        return false
      }
    })
  }

  private handleClose () { // 关闭抽屉
    this.drawer = false
  }
}
</script>
<style lang="stylus" scoped>
.time-step-box
   .dashed-box
      position relative
      overflow hidden
      background $bgc-gray
      padding 20px
      margin-bottom 20px
      border-radius 4px
     .single-time
       margin-bottom 20px
       &:last-child
         margin-bottom 0
       &:first-child
         margin-top 10px
  .add-periods,.add-point
    font-size 12px
    color $c-main
    cursor pointer
  .add-periods
    position absolute
    bottom -20px
    font-weight 500
    &:before
      margin-right 4px
  .add-point
    font-size 16px
    margin-left 10px
  .delete-point, .delete-stable
    color $icon-gray
    cursor pointer
    margin-left 10px
    &:hover
     color $c-danger
  .delete-periods
    position absolute
    top -18px
    right -18px
    background $c-danger
    color #fff
    width 36px
    height 36px
    display block
    text-align center
    border-radius 50%
    cursor pointer
    &:before
     content 'x'
     position absolute
     top 10px
     right 7px
     font-size 14px
     font-weight bold
     width 36px
     height 36px
.periods-box
  background $bgc-gray
  padding 20px
  margin-bottom 20px
  border-radius 4px
  .periods-item
    margin-bottom 20px
    &:last-child
      margin-bottom 0
    &:first-child
      margin-top 10px
</style>
